package com.zxshhy.beanLifecycle;

import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.beans.PropertyDescriptor;
import java.util.List;

@Slf4j
@Component
public class UserServiceImpl implements UserService , InstantiationAwareBeanPostProcessor, InitializingBean {
    public static List<String> list = Lists.newArrayList();

    @Override
    public void say() {
      log.info("\n大声说出来：生命周期");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        list.add(String.join("","\n初始化之前",beanName));
        return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        list.add(String.join("","\n初始化之后",beanName));
        return InstantiationAwareBeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        list.add(String.join("","\n实例化之前",beanName));
        return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        list.add(String.join("","\n实例化之后",beanName));
        return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        list.add(String.join("","\npostProcessProperties：",beanName));
        return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        list.add(String.join("","\npostProcessPropertyValues：",beanName));
        return InstantiationAwareBeanPostProcessor.super.postProcessPropertyValues(pvs, pds, bean, beanName);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        list.add(String.join("","\nafterPropertiesSet"));
    }

    @PostConstruct
    public void chut(){
        list.add(String.join("","\n调用init-method方法"));
    }
}
